from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as Ec
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import time

class pyse(object):
    def __init__(self, browser='chrome'):
        if browser.lower() == 'chrome':
            option = webdriver.ChromeOptions()
            option.add_argument('--start-maximized')
            print(option)
            driver = webdriver.Chrome(chrome_options=option)
        elif browser.lower() == 'firefox':
            driver = webdriver.Firefox()
        elif browser.lower() == 'ie':
            driver = webdriver.Ie
        elif browser.lower() == 'Safari':
            driver = webdriver.Safari
        self.driver = driver



    def wait_element(self, css, timeout=10, poll=0.5):
        if '=>' not in css:
            by = 'css'
            val = css
        else:
            by = css.split('=>')[0]
            val = css.split('=>')[1]

        if by == 'css':
            WebDriverWait(self.driver, timeout, poll).until(Ec.presence_of_element_located((By.CSS_SELECTOR, val)))
        if by == 'id':
            WebDriverWait(self.driver, timeout, poll).until(Ec.presence_of_element_located((By.ID, val)))
        if by == 'class':
            WebDriverWait(self.driver, timeout, poll).until(Ec.presence_of_element_located((By.CLASS_NAME, val)))
        if by == 'name':
            WebDriverWait(self.driver, timeout, poll).until(Ec.presence_of_element_located((By.NAME, val)))
        if by == 'xpath':
            WebDriverWait(self.driver, timeout, poll).until(Ec.presence_of_element_located((By.XPATH, val)))
        if by == 'link_text':
            WebDriverWait(self.driver, timeout, poll).until(Ec.presence_of_element_located((By.LINK_TEXT, val)))


    def get_element(self, css):

        if '=>' not in css:
            by = 'css'
            val = css
        else:
            by = css.split('=>')[0]
            val = css.split('=>')[1]

        self.wait_element(css)

        if by == 'css':
            element = self.driver.find_element_by_css_selector(val)
        if by == 'xpath':
            element = self.driver.find_element_by_xpath(val)
        if by == 'id':
            element = self.driver.find_element_by_id(val)
        if by == 'class':
            element = self.driver.find_element_by_class_name(val)
        if by == 'name':
            element = self.driver.find_element_by_name(val)
        if by == 'link_text':
            element = self.driver.find_element_by_link_text(val)
        return element

    def open(self, url):
        self.driver.get(url)

    def click(self, css):
        ele = self.get_element(css)
        ele.click()

    def type(self, css, text):
        ele = self.get_element(css)
        ele.send_keys(text)

    def max_window(self):
        self.driver.maximize_window()

    def clear(self, css):
        ele = self.get_element(css)
        ele.clear()

    def move_to_elemente(self, css):
        ele = self.get_element(css)
        ActionChains(self.driver).move_to_element(ele).perform()

    def drag_and_drop(self, source, targ):
        source_element = self.get_element(source)
        targ_element = self.get_element(targ)
        ActionChains(self.driver).drag_and_drop(source_element, targ_element).perform()

    def close(self):
        self.driver.close()

    def quit(self):
        self.driver.quit()

    def submit(self, css):
        ele = self.get_element(css)
        ele.submit()

    def refresh(self):
        self.driver.refresh()

    def js(self, scripe):
        self.driver.execute_script(scripe)

    def switch_to_frame(self, css):
        iframe_ele = self.get_element(css)
        self.driver.switch_to.frame(iframe_ele)

    def switch_to_parentFrame(self):
        self.driver.switch_to.parent_frame()

    def switch_to_frame_out(self):
        self.driver.switch_to.default_content()

    def select(self, css, value):
        ele = self.get_element(css)
        Select(ele).select_by_value(value)

    def deselect(self, css, value):
        ele = self.get_element(css)
        Select(ele).deselect_by_value(value)

    def deselectAll(self, css):
        ele = self.get_element(css)
        Select(ele).deselect_all()

    def get_screenshot(self, filePath):
        self.driver.get_screenshot_as_file(filePath)

    def open_new_window(self, i):
        self.driver.switch_to.window(self.driver.window_handles[i])

    def sleep(self, sec):
        time.sleep(sec)

    def wait(self, sec):
        self.driver.implicitly_wait(sec)

    def accept_alert(self,):
        self.driver.switch_to.alert.accept()

    def dismiss_alert(self):
        self.driver.switch_to.alert.dismiss()

    def get_alert_text(self):
        return self.driver.switch_to.alert.text





if __name__ == '__main__':
    driver = pyse('chrome')
    driver.open('http://ui.imdsx.cn/uitester/')
    # driver.get_element('css=>[placeholder="请通过CSS SELECTOR定位元素"]').send_keys('成功了')
    # driver.type('css=>[placeholder="请通过CSS SELECTOR定位元素"]', 'pyse')
    driver.js('window.scrollTo(0,0);')
    # driver.click('[id="alert"]')
    # print(driver.get_alert_text())
    # driver.accept_alert()
    # driver.dismiss_alert()
    # driver.click('link_text=>新建标签页面')
    # driver.open_new_window(1)
    # driver.js('window.scrollTo(0,0);')
    # driver.type('#newtag', 'sucessful')
    # driver.switch_to_frame('[name="baidu-frame"]')
    # driver.type('#kw', 'duladula')
    # driver.sleep(2)
    # driver.switch_to_frame_out()
    #
    # driver.sleep(2)
    # driver.close()
    # driver.sleep(2)
    # driver.open_new_window(0)
    # driver.move_to_elemente('.h')
    # driver.sleep(2)

    driver.click('[href="/move/"]')
    driver.open_new_window(1)
    driver.sleep(2)
    driver.drag_and_drop('#dragger', '#i1')
    driver.sleep(2)
    driver.quit()




